#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

//只能在堆上创建类，不能在栈上创建
class Computer{
public:
    Computer(const char* brand,int price)
    :_brand(new char[strlen(brand)+1]())
    ,_price(price)
    {
        strcpy(_brand,brand);
        cout << "Computer init" <<endl;
    }

    Computer & operator=(const Computer & rhs){
        if(this != &rhs){
            delete [] _brand;
            _brand = new char[strlen(rhs._brand)+1]();
            strcpy(_brand,rhs._brand);
            _price = rhs._price;
        }
        return *this;
    }
    void destroy(){
        delete this;
    }

private:
    ~Computer(){
        if(_brand){
            delete [] _brand;
            _brand = nullptr;
        }
        cout << "~Computer" <<endl;
    }
    
    char* _brand;
    int _price;
};

void test(){
    Computer *p1 = new Computer("rongyao",8000);
    //delete p1; //delete底层会先调用析构函数
    p1->destroy();
}

int main()
{
    test ();
    return 0;
}

